package com.xuyh.pdf.utils.ttjspider;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;

import java.sql.SQLException;
import java.util.List;

/**
 * @author xuyh0817
 * @version 1.0.0
 * @email xuyh0817@foxmail.com
 * @date 2024/9/24 12:27
 * @doc
 * @website
 * @description todo
 */
public class InsertAttachment {
    public void insert(List<Attachment> list) throws SQLException {

        for (Attachment attachment : list) {

            List<Entity> all = Db.use().findAll(Entity.create("cms_attachment").set("code", attachment.getCode()));
            if (CollUtil.isNotEmpty(all)) {
                attachment.setId(all.get(0).getLong("id"));
                continue;
            }
            Long id = Db.use().insertForGeneratedKey(
                    Entity.create("cms_attachment")
                            .set("type", attachment.getType())
                            .set("code", attachment.getCode())
                            .set("file_name", attachment.getFileName())
                            .set("file_size", attachment.getFileSize())
                            .set("file_type", attachment.getFileType())
                            .set("visit_path", attachment.getVisitPath())
                            .set("file_path", attachment.getFilePath())
                            .set("creator", attachment.getCreator())
                            .set("create_time", attachment.getCreateTime())
                            .set("updater", attachment.getUpdater())
                            .set("update_time", attachment.getUpdateTime())
                            .set("deleted", attachment.getDeleted())
                            .set("tenant_id", attachment.getTenantId())
            );
            attachment.setId(id);
        }
    }
}
